<#include "../common/header.ftl"/>
<script>
    $(function () {
        $(".inputBtn").click(function () {
            // 清空模态框
            $("#editForm")[0].reset();
            // 清除图片
            $(".coverUrl").attr("src", "");
            // 获取json数据
            var json = $(this).data("json");
            // console.log(json);
            // 将数据设置到模态框中
            if (json) {
                // 封面回显
                console.log(json);
                $("#coverUrl").val(json.coverUrl);
                $(".changeBtn").attr("src", json.coverUrl);

                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $(".coverUrl").attr("src",json.coverUrl);
                $("#editForm select[name='state']").val(json.state);
                ck.setData(json.content);
                $(".changeBtn").html(" 修改封面");
                // 查询文章的内容
                /*$.get("/strategyDetail/getContentById.do", {"id": json.id}, function (data) {
                    // 设置到富文本中
                    ck.setData(data.content);
                });*/
            } else {
                // 设置按钮文字
                $(".changeBtn").html(" 添加封面");
                // 清空富文本内容
                ck.setData("");
            }
            // 显示模态框
            $("#inputModal").modal("show");


        });
        // 提交表单
        $(".submitBtn").click(function () {
            //获取富文本编辑器的内容,设置到表单组件中
            var data = ck.getData();
            $("#editor").html(data);
            $("#editForm").ajaxSubmit(function (data) {
                doSuccess(data);
            });
        });

        // 更换封面
        // 封面选择
        $(".changeBtn").click(function () {
            $("#fileInput").click();
        });

        // 图片改变事件
        $("#fileInput").change(function () {
            // 有值才提交
            if ($(this).val()) {
                // 发送ajax请求
                $("#fileForm").ajaxSubmit({
                    url: "/images.do",
                    success: function (data) {
                        console.log(data);
                        if (data.uploaded == 1) {
                            // 成功
                            // 1.1 设置显示封面
                            $(".coverUrl").attr("src", data.url);
                            // 1.2 设置隐藏域
                            $("#coverUrl").val(data.url);
                        } else {
                            // 失败
                            layer.alert('亲，操作失败，请重试', {icon: 5});
                        }
                    }
                });
            }
        });

    });

</script>

</head>
<body>

<#include "../common/img.ftl"/>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#-- 往数据模型中填充一个属性 -->
            <#assign menu="newsPage"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/newsPage/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="标题/副标题"/>
                </div>
                <button class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn  btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>

                <#-- 循环迭代指令 -->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="50px"/></td>
                        <td>${entity.stateName}</td>
                        <td>${(entity.creadTime?string("yyyy-MM-dd"))!}</td>
                        <td>
                            <a class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>


<!-- 模态框必须作为body的子标签 -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/newsPage/saveOrUpdate.do" method="post" id="editForm">

                    <input type="text" name="id" hidden>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img class="coverUrl" width="100%"  class="changeBtn"/>
                            <a style="margin-left: 75px" class="btn btn-info glyphicon glyphicon-picture changeBtn"> 更换封面</a>
                            <input type="hidden" class="form-control" name="coverUrl" id="coverUrl">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="2">大热门</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="content" id="editor" rows="10" cols="80">
                        </textarea>
                        <script>
                            var ck = CKEDITOR.replace('editor');
                        </script>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    <span class="glyphicon glyphicon-off"></span> 关闭
                </button>
                <button type="button" class="btn btn-primary submitBtn">
                    <span class="glyphicon glyphicon-saved"></span> 保存
                </button>
            </div>
        </div>
    </div>
</div>


</body>
</html>